package com.easy_helth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.easy_helth.common.R;
import com.easy_helth.entity.Sport;
import com.easy_helth.service.CategoryService;
import com.easy_helth.service.SportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sport")
public class SportController {
    @Autowired
    SportService sportService;
    @Autowired
    CategoryService categoryService;

    /**
     * 新增运动
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Sport sport){
        sportService.save(sport);
        return R.success("新增运动成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){

        //构造分页构造器对象
        Page<Sport> pageInfo = new Page<>(page,pageSize);

        //条件构造器
        LambdaQueryWrapper<Sport> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name != null,Sport::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Sport::getUpdateTime);

        //执行分页查询
        sportService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }


    /**
     * 通过id获取菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Sport> get(@PathVariable int id){
        Sport sport = sportService.getById(id);
        return R.success(sport);
    }

    /**
     * 修改菜品信息
     * @param sport
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Sport sport){
        sportService.updateById(sport);
        return R.success("修改成功");
    }


    /**
     * 删除菜品
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable int id){
        sportService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 返回套餐列表
     * @param sport
     * @return
     */
    @GetMapping("/list")
    public R<List<Sport>> list(Sport sport){
        Integer id = sport.getId();
//        构造查询条件
        LambdaQueryWrapper<Sport> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(id != 0,Sport::getId,sport.getId());
//        只查状态为1
        queryWrapper.eq(Sport::getStatus,1);
        queryWrapper.eq(Sport::getCategoryId,sport.getCategoryId());
        queryWrapper.orderByDesc(Sport::getSort).orderByDesc(Sport::getUpdateTime);
        List<Sport> sportList = sportService.list(queryWrapper);
        return R.success(sportList);
    }

    /**
     * 修改套餐状态
     * @param id
     * @return
     */
    @PostMapping("/status/{id}")
    public R<Sport> updateState(@PathVariable int id){
        Sport sport = sportService.getById(id);
        Integer status = sport.getStatus();
        if (status == 0) {
            sport.setStatus(1);
        }
        else
            sport.setStatus(0);
        return R.success(sport);
    }

    @GetMapping("/getList/{categotyId}")
    public R<List<Sport>> getSportListByCategoryId(@PathVariable int categotyId){
        LambdaQueryWrapper<Sport> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Sport::getCategoryId,categotyId);
        List<Sport> sportList = sportService.list(lambdaQueryWrapper);
        return R.success(sportList);
    }
}
